#ifndef 	_EZ_def_H
#define 	_EZ_def_H


#include "stdint.h"
#include "EZ_Thread_config.h"

#define EZ_THREAD_INIT                  0x00                /**< Initialized status */
#define EZ_THREAD_READY                 0x01                /**< Ready status */
#define EZ_THREAD_SUSPEND               0x02                /**< Suspend status */
#define EZ_THREAD_RUNNING               0x03                /**< Running status */
#define EZ_THREAD_BLOCK                 RT_THREAD_SUSPEND   /**< Blocked status */
#define EZ_THREAD_CLOSE                 0x04                /**< Closed status */

#define EZ_TIMER_INIT										0x00
#define EZ_TIMER_ONCE										0x01
#define EZ_TIMER_CYCLE									0x02
#define EZ_TIMER_STOP										0x03

#define EZ_TIME_CTRL_SET_TIME						0x04
#define EZ_TIME_CTRL_SET_FLAG						0x05


#define EZ_TRUE													(0x01)
#define EZ_ERR													(0x00)

#define THREAD_PRIORITY_MAX							(32)

#define EZ_NULL													(0x00)

#define NVIC_INT_CTRL       						(0xE000ED04)     
#define NVIC_PENDSVSET      						(0x10000000)      
#define NVIC_SYSPRI2        						(0xE000ED22)      
#define NVIC_PENDSV_PRI     						(0x000000FF)      

#define MEM32(addr)         *(volatile unsigned long *)(addr)
#define MEM8(addr)          *(volatile unsigned char *)(addr)




struct exception_stack_frame
{
    uint32_t r0;
    uint32_t r1;
    uint32_t r2;
    uint32_t r3;
    uint32_t r12;
    uint32_t lr;
    uint32_t pc;
    uint32_t psr;
};

struct stack_frame
{
#if USE_FPU
    uint32_t flag;
#endif /* USE_FPU */

    /* r4 ~ r11 register */
    uint32_t r4;
    uint32_t r5;
    uint32_t r6;
    uint32_t r7;
    uint32_t r8;
    uint32_t r9;
    uint32_t r10;
    uint32_t r11;

    struct exception_stack_frame exception_stack_frame;
};

#pragma pack(1)
struct list
{
	struct list *next;
	struct list *prev;
};
#pragma pack()


typedef struct list list_t;

struct single_list
{
	struct single_list *next;
};

typedef struct single_list s_list_t;

struct ez_object
{
	char 		name[EZ_NAME_SIZE];
	uint8_t type;
	uint8_t flag;
	list_t	list;
};

typedef struct ez_object ez_object_t,*ez_object_p;

struct ez_timer
{
	ez_object_t	object;
	list_t	tlist; 
	
	void (*timeout_fun)(void *param);
	void *param;
	
	uint32_t init_tick;
	uint32_t start_tick;
	
  uint32_t flag;

};

typedef struct ez_timer ez_timer_t,*ez_timer_p;

struct ez_thread
{

	void      	*sp;                                     /**< stack point */
  void       	*entry;                                  /**< entry */
  void       	*parameter;                              /**< parameter */
  void       	*stack_addr;                             /**< stack address */
	void				*thread_exit;
  uint32_t 		stack_size;                             /**< stack size */
	
	list_t 			tlist;
	
	uint8_t		  current_priority;                       /**< current priority */
	uint8_t  		init_priority;  
	
	uint32_t	  init_tick;                              /**< thread's initialized tick */
	uint32_t	  remaining_tick;   

	uint32_t 		stat;
	
	uint32_t  	sleep_tick;
	ez_timer_t	timer;
	
	void 				(*cleanup)(struct ez_thread *tid); 
	
	ez_object_t object;


};

typedef struct ez_thread ez_thread_t,*ez_thread_p;

struct ipc_object
{
	ez_object_t object;
	list_t 			suspend_thread;
};

typedef struct ipc_object ez_ipc_t,*ez_ipc_p;

struct ipc_sem_object
{
	ez_ipc_t 		ipc;
	uint32_t 		vaule;
};

typedef struct ipc_sem_object ez_ipc_sem_t,*ez_ipc_sem_p;

struct ez_memport_object
{
	ez_ipc_t 	ipc;
  uint8_t * memStart;
  uint32_t  memSize;
  list_t    blockList;
};

typedef struct ez_memport_object ez_memport_t,*ez_memport_p;

#endif
